import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
import pickle
%matplotlib inline
from util import image_process as img_tf
from util import camera as camera
# Create 'output_images' folder if it doesn't exist
out_images_folder = 'output_images'
if not os.path.exists(out_images_folder):
os.makedirs(out_images_folder)
print('Output folder output_images created to store image processing results!')
# Specify the output directory to save image and create one if does not exist
CALIB_IMAGES = "./camera_cal/*.jpg"
CALIB_IMAGE_OUT_DIR = "./output_images/camera_calibration"
if not os.path.exists(CALIB_IMAGE_OUT_DIR):
os.makedirs(CALIB_IMAGE_OUT_DIR)
# Prepare object points
nx = 6 # Number of inside corners in x
ny = 9 # Number of inside corners in y
nz = 3 # Number of channels
print('Calibrating Camera...........')
cameraMatrix, distortionCoeffs = camera.calibrate_camera(CALIB_IMAGES, nx, ny, nz, CALIB_IMAGE_OUT_DIR)
print('Calibration Done!')
from util import image_process as ip
print('Undistorting Camera Calibration Images...........')
# Make a list of calibration images
calib_images = glob.glob(CALIB_IMAGES)
for idx, img in enumerate(calib_images):
image_name = img.split('/')[2].split('.')[0]
image = mpimg.imread(img)
undist = ip.undistort(image, cameraMatrix, distortionCoeffs, image_name, CALIB_IMAGE_OUT_DIR)
test_images = glob.glob('./test_images/*.jpg')
TEST_IMAGE_OUT_DIR = "./output_images/test_images_out"
if not os.path.exists(TEST_IMAGE_OUT_DIR):
os.makedirs(TEST_IMAGE_OUT_DIR)
print('Undistorting Input Images...........')
for idx, img in enumerate(test_images):
image_name = img.split('/')[2].split('.')[0]
out_dir = os.path.join(TEST_IMAGE_OUT_DIR, image_name)
image = mpimg.imread(img)
undist = ip.undistort(image, cameraMatrix, distortionCoeffs, image_name, out_dir)
print('Visualizing Image spaces on Test Images...........')
for idx, img in enumerate(test_images):
image_name = img.split('/')[2].split('.')[0]
image = mpimg.imread(img)
print('Image: {}\n----------------------------------------------------------------'.format(image_name))
out_dir = os.path.join(TEST_IMAGE_OUT_DIR, image_name)
# Get RGB
fig = ip.get_color_channels(image, color_space = 'RGB', image_name=image_name, plot = True)
plt.savefig('{}/{}.jpg'.format(out_dir, image_name + '_RGB'))
# Get HSV
fig = ip.get_color_channels(image, color_space = 'HSV', image_name=image_name, plot = True)
plt.savefig('{}/{}.jpg'.format(out_dir, image_name + '_HSV'))
# Get HLS
fig = ip.get_color_channels(image, color_space = 'HLS', image_name=image_name, plot = True)
plt.savefig('{}/{}.jpg'.format(out_dir, image_name + '_HLS'))
plt.show()
print('Applying binary threshold on test images.............')
for idx, img in enumerate(test_images):
image_name = img.split('/')[2].split('.')[0]
print('Image: {}\n----------------------------------------------------------------'.format(image_name))
out_dir = os.path.join(TEST_IMAGE_OUT_DIR, image_name)
new_image = mpimg.imread(img)
undist = ip.undistort(new_image, cameraMatrix, distortionCoeffs)
thresholded = ip.get_binary_thresholded_image(undist, image_name, out_dir)
plt.show()
print('Applying perspective transform on warped image......')
for idx, img in enumerate(test_images):
image_name = img.split('/')[2].split('.')[0]
new_image = mpimg.imread(img)
print('Image: {}\n----------------------------------------------------------------'.format(image_name))
out_dir = os.path.join(TEST_IMAGE_OUT_DIR, image_name)
undist = ip.undistort(new_image, cameraMatrix, distortionCoeffs)
thresholded = ip.get_binary_thresholded_image(undist)
warped, M_inv = ip.apply_warp(thresholded, image_name, out_dir)
image_size = (warped.shape[1], warped.shape[0])
plt.show()
from util import lane_detection as lane_det
test_images = glob.glob('./test_images/*.jpg')
for idx, img in enumerate(test_images):
image_name = img.split('/')[2].split('.')[0]
orig_image = mpimg.imread(img)
undist = ip.undistort(orig_image,cameraMatrix, distortionCoeffs)
thresholded = ip.get_binary_thresholded_image(undist)
warped, M_inv = ip.apply_warp(thresholded)
image_size = (warped.shape[1], warped.shape[0])
print('Image: {}\n----------------------------------------------------------------'.format(image_name))
out_dir = os.path.join(TEST_IMAGE_OUT_DIR, image_name)
print('Pixel histograms of the warped image......')
leftx_base, rightx_base = lane_det.plot_histogram(warped, image_name, out_dir)
plt.show()
print('Applying sliding window search for lane detection......')
left_fit_p, right_fit_p, left_lane_inds, right_lane_inds = lane_det.polyfit_sliding_window(warped, image_name, out_dir)
plt.show()
print('Applying polyfit for lane detection using the information from previous frame.\n In this case from last sliding window search......')
left_fit_p, right_fit_p, left_lane_inds, right_lane_inds= lane_det.polyfit_using_previous_fit(warped, left_fit_p, right_fit_p, image_name, out_dir)
plt.show()
img_out = lane_det.shade_lane_area(orig_image, warped, left_fit_p, right_fit_p, M_inv, image_name, out_dir)
curve_rad = lane_det.measure_radius_of_curvature(warped, left_fit_p, right_fit_p, left_lane_inds, right_lane_inds)
center_offset = lane_det.measure_offset_from_center(warped, left_fit_p, right_fit_p)
print('Shaded lane image with distance parameters annotated......')
img_out = lane_det.add_distance_text_to_image(img_out, curve_rad, center_offset, image_name, out_dir)
plt.imshow(img_out)
plt.show()